﻿///////////////////////////////////////////////////////////
//  ImageLoad.as
//  Created on:      23-07-2009 14:39:25
//  Original author: yes3d
///////////////////////////////////////////////////////////
package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	//import flash.events.ProgressEvent;
	//import flash.text.TextField;
	//import flash.text.TextFieldAutoSize;
	/**
	 * @author yes3d
	 * @version 1.0
	 * @created 23-07-2009 14:39:25
	 */
	public class OrigImage extends Sprite
	{
		private var _bmp:*;
		private var _center:Boolean;
		private var _bitmap:Bitmap;
		//初始化TextField   
		//private var _textField:TextField;
		public function OrigImage(center:Boolean=true)
		{
			super();
			_center=center;
		}
	    /**
	     * @param imageUrl 设置图片路径
	     */
	    public function set path(imageUrl:String): void
	    {
	    	if(_bmp)
	    	{
				dispose();
	    	}
			//_textField=new TextField()
			//this.addChild(_textField);
			_bmp=new OrigBmp();
			_bmp.addEventListener(Event.COMPLETE,loadedHandle);
		//	_bmp.addEventListener(ProgressEvent.PROGRESS,onProgress);
			_bmp.path=imageUrl;
	    }/*
		private function onProgress(i:int):Function //进度监听处理   
		{   
			var fun=function(e:ProgressEvent):void 
			{   
				var loadtotal:uint=e.bytesTotal;   
				var loaded:uint=e.bytesLoaded;   
				var perc:String=Math.round((loaded/loadtotal)*100)+"%";   
				
				_textField.text=" 加载中…………"+perc;   
			}   
			
			return fun;   
			
		}   
		
		*/
		public function set bmpData(bmp:BitmapData):void
		{
			_bmp=new Bitmap(bmp);
			localBitmap();
		}
		
		public function get bmpData():BitmapData
		{
			if(_bmp==null) _bmp=new Bitmap(new BitmapData(100,100,false,0x0));
			return _bmp.bitmapData;
		}
		/**
		 * 加载完成,使注册点居中
		 */
		 private function loadedHandle(event:Event):void
		 {
			 _bmp.removeEventListener(Event.COMPLETE,loadedHandle);
			 this.addEventListener(Event.REMOVED_FROM_STAGE,dispose);
			 localBitmap();
		 }
		 private function localBitmap():void
		 {
			 if(_center)
			 {
				 _bmp.x=-_bmp.width/2;
				 _bmp.y=-_bmp.height/2;
			 }
			 
			 this.addChild(_bmp);
			 this.dispatchEvent(new Event(Event.COMPLETE));
		 }
		 /*
		 public function clearMe():void
		 {
			 if(_bmp)
			 {
			 try{
				_bmp.bitmapData.dispose();
				 removeChild(_bmp);
			 } catch (e:Error){
				 trace("orifimage clearMe is Error")
			 }
			 _bmp=null;
			 }
		 }*/
		 public function dispose(event:Event=null):void
		 {
			 this.removeEventListener(Event.REMOVED_FROM_STAGE,dispose);
			 if(_bmp)
			 {
				 _bmp.removeEventListener(Event.COMPLETE,loadedHandle);
				 try{
				 _bmp.bitmapData.dispose();
				 removeChild(_bmp);
				 _bmp=null;
				 }catch (e:Error){}
			 }
		 }
	}
}